<?php
// $Id$

/**
 * Controller_Admin 控制器
 */
class Controller_Admin extends Controller_Abstract
{

	function actionIndex()
	{
        // 为 $this->_view 指定的值将会传递数据到视图中
		# $this->_view['text'] = 'Hello!';
            $page = intval($this->_context->page);
            if($page<1){
               $page=1;
            }
           
            $article = Article::find();
            $article->limitpage($page, 15);
            
            $this->_view['pagination'] = $article->getPagination();
            $this->_view['articles'] = $article->getAll();
	}
        
        function actionInfo()
        {
            
        }
        
        function actionAdd()
        {
            if($this->_context->isPOST()){
             // print_r($_POST);
                $article = new Article();
                $article->title = $_POST['title'];
                $article->keywords = $_POST['keywords'];
                $article->description = $_POST['description'];
                $article->body = $_POST["body"];
                $article->own = "admin";
                $article->save();
                return $this->_redirect(url('admin/'));
            }  
        }
        
        function actionEdit()
        {
            $article = Article::find('id = ? ',$this->_context->id )->query();
            
            if(!$article->id){
                return $this->_redirect(url('admin/index'));
            }
            if($this->_context->isPOST()){
               //print_r($_PO/ST);
               
                $article->title = $_POST['title'];
                $article->keywords = $_POST['keywords'];
                $article->description = $_POST['description'];
                $article->body = $_POST['body'];
                $article->save();
                return $this->_redirect(url('admin/index'));
            }
              $this->_view['article'] = $article;
        }
        
        function actionDel()
        {
            Article::meta()->destroyWhere('id=?',$this->_context->id);
            return $this->_redirectMessage("Del action", "Del success!", url('admin/index'));
        }
        
        function actionArticleList()
        {
            $page = intval($this->_context->page);
            if($page<1){
               $page=1;
            }
           
            $article = Article::find();
            $article->limitpage($page, 15);
            
            $this->_view['pagination'] = $article->getPagination();
            $this->_view['articles'] = $article->getAll();
        }
        
        function actionLogin()
        {
            if($this->_context->isPOST()){
                try{
                  $user = User::meta()->validateLogin($_POST['username'],$_POST['password']);
                   
                    $this->_app->changeCurrentUser($user->aclData(), 'MEMBER');
                    return $this->_redirect(url('admin/index'));
              }
                catch (AclUser_UsernameNotFoundException $ex){
                      return $this->_redirectMessage('error', '用户名出错', url('default/index'));
                }
                catch(AclUser_WrongPasswordException $ex){
                     return $this->_redirectMessage('error', '密码出错', url('default/index'));   
                }
            
            }
        }
        
        function actionLogout()
        {
            $this->_app->cleanCurrentUser();
            return $this->_redirect(url('default/index'));
        }
        
        function actionUserinfo()
        {
           if($this->_context->isPOST()){
              try{
                    $user = $this->_app->currentUser();
                    User::meta()->changePassword($user['username'], $_POST['new_password'], $_POST['old_password']);
                    return $this->_redirectMessage('Success', 'you can use new password.', url('admin/index'));
              }
              catch (AclUser_WrongPasswordException $ex){
                    return $this->_redirectMessage('Error', 'old password Error', url('default/index'));
             }
           }
        }
        
        function actionUserList()
        {
            $users = User::find()->getAll();
            $this->_view['users'] = $users;
        }
        
        function actionUserDel()
        {
            User::meta()->destroyWhere('uid=?',$this->_context->uid);
            return $this->_redirect(url('admin/userlist'));
        }
        
        function actionRegister()
        {
            if($this->_context->isPOST()){
                $user = new user();
                $user->username = $_POST['username'];
                $user->password = $_POST['password'];
                $user->rank = 0;
                $user->save();
                return $this->_redirect(url('admin/index'));
                
            }
        }
     
    
}


